package com.itheima.manager;

import com.itheima.domain.FlashPromotionSession;
import com.itheima.service.SmsFlashPromotionSessionService;
import com.itheima.util.Result;
import com.itheima.vo.SmsFlashPromotionSessionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class SmsFlashPromotionSessionManager {

    @DubboReference
    SmsFlashPromotionSessionService sessionService;

    //获取全部场次
    public Result list() {
        List<FlashPromotionSession> sessionList = sessionService.list();
        return new Result(true, 200, "操作成功", sessionList);
    }

    //添加场次
    public Result create(FlashPromotionSession promotionSession) {

        //判断开始时间是否小于结束时间
        if (promotionSession.getStartTime().isAfter(promotionSession.getEndTime())) {
            return new Result(false, 403, "操作失败，开始时间大于结束时间", 1);
        }
        promotionSession.setCreateTime(LocalDateTime.now());
        sessionService.create(promotionSession);
        return new Result(true, 200, "操作成功", 1);
    }

    //获取全部可选场次及其数量
    public Result selectList(Integer flashPromotionId) {
        List<SmsFlashPromotionSessionVo> voList = sessionService.selectList(flashPromotionId);
        return new Result(true, 200, "操作成功", voList);
    }

    //获取场次详情
    public Result selectById(Integer id) {

        FlashPromotionSession smsFlashPromotionSession = sessionService.selectById(id);
        return new Result(true, 200, "操作完成", smsFlashPromotionSession);
    }

    //删除场次
    public Result delete(Integer id) {
        sessionService.delete(id);
        return new Result(true, 200, "操作成功", 1);
    }

    //修改启用状态
    public Result updateStatus(Integer id, Integer status) {
        sessionService.updateStatus(id, status);
        return new Result(true, 200, "操作成功", 1);
    }

    //修改场次
    public Result updateById(Integer id, FlashPromotionSession promotionSession) {
        //判断开始时间是否小于结束时间
        if (promotionSession.getStartTime().isAfter(promotionSession.getEndTime())) {
            return new Result(false, 403, "操作失败，开始时间大于结束时间", 1);
        }
        promotionSession.setId(Long.valueOf(id));
        sessionService.updateById(promotionSession);
        return new Result(true, 200, "操作成功", 1);
    }

}
